package com.gitee.fantaros.web.fan.netcut.threading;

import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;

public interface Syncable<T, P> extends Callable<T> {

    default T call() throws Exception {
        markStarted();
        SyncExecutor<T, P> executor = getExecutor();
        T t = executor.execute(getProcessable(), getResultWrapper(), getAnyException());
        if (getResultWrapper().isEmpty()) {
            getResultWrapper().set(t);
        }
        markFinished(t);
        return t;
    }

    SyncExecutor<T,P> getExecutor();
    void setExecutor(SyncExecutor<T, P> executor);

    Processable<P> getProcessable();
    Presentable<T> getResultWrapper();
    Presentable<Exception> getAnyException();
    void markStarted();
    void markFinished(T t);

    void setCountDownLatch(CountDownLatch countDownLatch);

    void setProcessable(Processable<P> processable);
    void setResultWrapper(Presentable<T> resultWrapper);
    void setAnyException(Presentable<Exception> anyException);

}
